package TUILayer;
import java.util.Scanner;
import ControlLayer.LoanCtr;

public class LoanUI
{
    private LoanCtr loanCtr;//DVD UI must know the DVD controller 

    private long dvdId;
    private long sampleSerial;
    private long personId;
    private String date;
    private int periode;
    private boolean status;
    private long id;

    public LoanUI()
    {

    }

    public void start()
    {
        loanCtr = new LoanCtr();
        loanMenu();
    }

    public void loanMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeLoanMenu();
            if (choice == 1)
            {
                findLoan();
            }
            else{
                if(choice == 2){
                    createLoan();
                }
                else{
                    if(choice == 3){
                        updateLoan();
                    }
                    else{
                        if(choice == 4){
                            returnLoan();
                        }
                        else{
                            
                                        if(choice == 5){
                                            listAllLoans();
                                        }
                                        else{
                                            exit = true;
                                        }//end else
                                    }//end else
                                }//end else
                            }//end else
                        }//end else

                    }//end while
                }
            
    private int writeLoanMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Loan Menu ***");
        System.out.println(" (1) Show Loan information");
        System.out.println(" (2) Create Loan");
        System.out.println(" (3) Update Loan");
        System.out.println(" (4) Delete Loan");
        System.out.println(" (5) Show current loan list");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }
    
    private String inputDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type date for rent: " );
        String date = keyboard.nextLine();
        return date;
    }
    
    public int inputLoanPeriode()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Type periode for loan:  ");
            try{
                periode = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return periode;
    }
    
    private boolean inputLoanStatus()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the status of loan (true=lend out /false=in stock: ");
        boolean status = keyboard.nextBoolean();
        return status;
    }
    
    public long inputLoanNumber()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Type ID of loan:  ");
            try{
                id = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
    }
    
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Type persons ID:  ");
            try{
                personId = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return personId;
    }
    
    public long inputDVDNo()
    {   
        boolean ok = false;
        long dvdId = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Type the DVDs number:  ");
            try{
                dvdId = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return dvdId;
    }
    
    public long inputCopyNo()
    {   
        boolean ok = false;
        long sampleSerial = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Type the copys number:  ");
            try{
                sampleSerial = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return sampleSerial;
    }
    
    public void inputDefault()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Press ENTER to continue: ");
        String enter = keyboard.nextLine();
    }
    
        private void createLoan()
    {
        // Creates a new loan
        date = inputDate();
        periode = inputLoanPeriode();
        status = inputLoanStatus();
        id = inputLoanNumber();
        personId = inputPersonsID();
        dvdId = inputDVDNo();
        sampleSerial = inputCopyNo();
        
        loanCtr.createLoan(date, periode, status, id, personId, dvdId, sampleSerial);
        System.out.println("");
        inputDefault();
    }
    
    private void findLoan()
    {
        //Prints the information about a loan
        id = inputLoanNumber();        
        loanCtr.getLoan(id);
        System.out.println("");
        inputDefault();
    }
    
    private void updateLoan()
    {
        //Edites the information about a loan
        date = inputDate();
        periode = inputLoanPeriode();
        status = inputLoanStatus();
        id = inputLoanNumber();        
        loanCtr.updateLoan(date, periode, status, id);
        System.out.println("");
        inputDefault();
    }
    
    private void returnLoan()
    {
        //Returns the copy of a dvd, by deleting the loan
        id = inputLoanNumber();        
        loanCtr.returnLoan(id);
        System.out.println("");
        inputDefault();
    }
    
    private void listAllLoans()
    {
        // Prints the information about all loans
        loanCtr.listAllLoans();
        System.out.println("");
        inputDefault();
    }
}